/**
 * @(#)ApkConfigController.java 2017年03月1日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;
import java.util.List;
import java.util.Map;

import org.jsbd.boss.domian.HeartbeatApkConfigBean;
import org.jsbd.boss.domian.channel.FileUploadVo;
import org.jsbd.boss.service.FileManager;
import org.jsbd.boss.service.IHeartbeatApkConfigService;
import org.jsbd.boss.common.ConstantsCmp;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseMapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.dubbo.common.utils.StringUtils;


@Controller
@RequestMapping("/heartbeatApkConfig")
public class HeartbeatApkConfigController{

	private static final Logger LOGGER=LoggerFactory.getLogger(HeartbeatApkConfigController.class);
	
	@Autowired
	private IHeartbeatApkConfigService heartbeatApkConfigService;
	@Autowired
	private FileManager fileManager;
	
	private static final String PREFIX="heartbeat";

	/**
	 * 列表页面
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "listModel")
	public String showHeartbeatApkConfigList(Model model) throws Exception {
		return "/heartbeatApkConfig/heartbeatApkConfigList";
	}

	@RequestMapping(value = "saveHeartbeatApkConfig")
	@ResponseBody
	public Object saveHeartbeatApkConfig(HeartbeatApkConfigBean heartbeatApkConfig,@RequestParam(value = "heartfile",required = true) MultipartFile filepath){
		if(StringUtils.isBlank(heartbeatApkConfig.getVersion())){
			return ResponseMapUtils.responseFail("版本号不能为空");
		}
		
		HeartbeatApkConfigBean condition=new HeartbeatApkConfigBean();
		condition.setIsDelete(0);
		condition.setVersion(heartbeatApkConfig.getVersion());
		HeartbeatApkConfigBean  validate=heartbeatApkConfigService.getHeartbeatApkConfig(condition);
		if(validate!=null){
			return ResponseMapUtils.responseFail("版本号已经存在");
		}
		
		FileUploadVo fileVo=fileManager.uploadFile(filepath, ConstantsCmp.BUCKET_NAME, PREFIX);
		heartbeatApkConfig.setMd5(fileVo.getMd5());
		heartbeatApkConfig.setIsDelete(0);
		heartbeatApkConfig.setFilepath(fileVo.getFilePath());
		heartbeatApkConfig.setBucketName(ConstantsCmp.BUCKET_NAME);
		heartbeatApkConfigService.saveHeartbeatApkConfig(heartbeatApkConfig);
		return ResponseMapUtils.responseSuccess();
	}

	@RequestMapping(value = "updateHeartbeatApkConfig")
	@ResponseBody
	public Object updateHeartbeatApkConfig(HeartbeatApkConfigBean heartbeatApkConfig){
		if (heartbeatApkConfig.getId() == null) {
			return ResponseMapUtils.responseFail("参数缺失，请确认参数再提交");
		}
		
		HeartbeatApkConfigBean condition=new HeartbeatApkConfigBean();
		condition.setIsDelete(0);
		condition.setId(heartbeatApkConfig.getId());
		HeartbeatApkConfigBean temp=heartbeatApkConfigService.getHeartbeatApkConfig(condition);
		if(temp==null){
			return ResponseMapUtils.responseFail("记录未找到,请重试");
		}
		
		heartbeatApkConfigService.updateHeartbeatApkConfig(heartbeatApkConfig);
		return ResponseMapUtils.responseSuccess();
	}

	@RequestMapping(value = "getHeartbeatApkConfig")
	@ResponseBody
	public HeartbeatApkConfigBean getHeartbeatApkConfig(HeartbeatApkConfigBean heartbeatApkConfig) throws Exception {
		return heartbeatApkConfigService.getHeartbeatApkConfig(heartbeatApkConfig);
	}

	@RequestMapping(value = "getHeartbeatApkConfigList")
	@ResponseBody
	public List<HeartbeatApkConfigBean> getHeartbeatApkConfigList(HeartbeatApkConfigBean heartbeatApkConfig) throws Exception {
		return heartbeatApkConfigService.getHeartbeatApkConfigList(heartbeatApkConfig);
	}

	@RequestMapping(value = "getHeartbeatApkConfigListPage")
	@ResponseBody
	public Map<String, Object> getHeartbeatApkConfigListPage(DataGridModel page, HeartbeatApkConfigBean heartbeatApkConfig) throws Exception {
		return heartbeatApkConfigService.getHeartbeatApkConfigListPage(page, heartbeatApkConfig);
	}
	
	@RequestMapping(value = "deleteHeartbeatApkConfig")
	@ResponseBody
	public Map<String, Object> deleteHeartbeatApkConfig(HeartbeatApkConfigBean heartbeatApkConfig) throws Exception {
		heartbeatApkConfig.setIsDelete(1);
		heartbeatApkConfigService.deleteHeartbeatApkConfig(heartbeatApkConfig);
		return ResponseMapUtils.responseSuccess();
	}
	

}
